﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows.Media;

namespace GraphicsDesigner
{
    public class Triangle : Shapes
    {
        #region Members

        Point3D point0;
        Point3D point1;
        Point3D point2;

        MeshGeometry3D triangleMesh;

        #endregion

        #region Private Members

        private static Material SetColor(Color color)
        {
            Material material = new DiffuseMaterial(
                new SolidColorBrush(color));
            return material;
        }

        private void AllocateNormal()
        {
            Vector3D normal = new Vector3D(0, 1, 0);

            triangleMesh.Normals.Add(normal);
            triangleMesh.Normals.Add(normal);
            triangleMesh.Normals.Add(normal);
        }

        private void AllocateIndices()
        {
            triangleMesh.TriangleIndices.Add(0);
            triangleMesh.TriangleIndices.Add(2);
            triangleMesh.TriangleIndices.Add(1);
        }

        private void AllocatePosition()
        {
            triangleMesh.Positions.Add(point0);
            triangleMesh.Positions.Add(point1);
            triangleMesh.Positions.Add(point2);
        }

        private void InitializePoints(int size)
        {
            point0 = new Point3D(0, 0, 0);
            point1 = new Point3D(size, 0, 0);
            point2 = new Point3D(0, 0, size);
        }

        #endregion

        #region Public Members

        /// <summary>
        /// Draws a triangle with specified size and color.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ModelVisual3D Draw(int size,Color color)
        {
            InitializePoints(size);

            triangleMesh = new MeshGeometry3D();

            AllocatePosition();

            AllocateIndices();

            AllocateNormal();

            Material material = SetColor(color);
            
            GeometryModel3D triangleModel = new GeometryModel3D(
                triangleMesh, material);

            ModelVisual3D model = new ModelVisual3D();

            model.Content = triangleModel;
            return model;
        }

        #endregion


    }
}
